﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;

using edowork.com.SQL2005;

namespace WebInvoicingsys.basecontrol.business
{
    public partial class uc_from_dates : System.Web.UI.UserControl
    {
        public string getValue()
        {
            return requestedDeliveryDateTextBox.Text.Trim();
        }

        public void setValue(string values)
        {
            DateTime _values;

            if (DateTime.TryParse(values, out _values) == false)
            {
                _values = DateTime.Now;
            }

            requestedDeliveryDateTextBox.Text = _values.ToString("yyyy-MM-dd");
            requestedDeliveryDateCalendar.SelectedDate = _values;
        }

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "select max(sysmid) sysmid, years, months from sys_monthly_main group by years, months";
            string error_msg = "";

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);
                      
            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                //当入当前月份
                try
                {
                    Session["monthly"] = DateTime.Now.ToString("MM");
                    Session["years"] = DateTime.Now.ToString("yyyy");
                }
                catch
                {
                    Session.Add("monthly", DateTime.Now.ToString("MM"));
                    Session.Add("years", DateTime.Now.ToString("yyyy"));
                }
            }
            else
            {
                //当入当前月份
                try
                {
                    Session["monthly"] = ds.Tables[0].Rows[0]["months"].ToString();
                    Session["years"] = ds.Tables[0].Rows[0]["years"].ToString();
                }
                catch
                {
                    Session.Add("monthly", ds.Tables[0].Rows[0]["months"].ToString());
                    Session.Add("years", ds.Tables[0].Rows[0]["years"].ToString());
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();

                requestedDeliveryDateTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 日期选择图标被点击
        /// </summary>
        protected void ImageButton_Click(object sender, EventArgs eventArgs)
        {
            //控制日历的显示与隐藏
            calendar.Visible = !calendar.Visible;
        }

        /// <summary>
        /// 选择日期，通过AJAX触发
        /// </summary>
        protected void RequestedDeliveryDateCalendar_SelectionChanged(object sender, EventArgs eventArgs)
        {
            DateTime select_dates = requestedDeliveryDateCalendar.SelectedDate;

            DateTime close_dates = DateTime.Parse(Session["years"].ToString() + "-" + Session["monthly"].ToString() + "-01").AddMonths(1);

            //比较两个日期之间的差
            if (close_dates.CompareTo(select_dates) > 0)
            {
                requestedDeliveryDateTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {
                requestedDeliveryDateTextBox.Text = select_dates.ToString("yyyy-MM-dd");
            }
            
            // 隐藏日历
            calendar.Visible = false;

            //设置日历下textbox的焦点，方便用户输入。移除或改变下行代码设置为您自己的控件
            requestedDeliveryDateTextBox.Focus();
        }
    }
}